<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登录表单区域 -->

      <el-form :rules="loginFormRules" class="login_form" :model="loginForm" ref="loginFormRef">
        <!-- name -->
        <el-form-item prop="userName">
          <el-input
            prefix-icon="iconfont icon-mima"
            v-model="loginForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- pwd -->
        <el-form-item prop="userPassWord">
          <el-input
            prefix-icon="iconfont icon-yonghuming"
            v-model="loginForm.userPassWord"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="loginReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
// import Api from "../netword/Api"
export default {
  data() {
    return {
      loginForm: {
        userName: "admin",
        userPassWord: "123456"
      },
      //表单验证规则
      loginFormRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        userPassWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods:{
    loginReset(){
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate( valid =>{
        // console.log(this);
        //console.log("valid:"+valid);
        if(!valid) {return };
        this.$api.loginApi(this.loginForm.userName,this.loginForm.userPassWord)
        .then( res =>{
          // var data =JSON.parse(res)
          console.log("res:"+JSON.stringify(res));
          if(res.meta.status != 200 ){
            // console.log("res:"+JSON.stringify(res.meta.msg));
            this.$message.error(res.meta.msg);
          }else{
            // console.log("token:"+JSON.stringify(res.data.token));
            // console.log("username:"+JSON.stringify(res.data.username));
            // this.$message(res.meta.msg);
            this.$message.success(res.meta.msg);
            window.sessionStorage.setItem("token",res.data.token);
            this.$router.push("/home");

          }
        })
        .catch(err =>{
          console.log("err:"+err);
        });

      })
    }
  }
};
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -150px;
  margin-left: -225px;

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 50px;
  box-sizing: border-box;
}
</style>
